package com.tsl.prevention.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsl.prevention.modules.user.dao.InfoMapper;
import com.tsl.prevention.modules.user.entity.Info;
import com.tsl.prevention.modules.user.entity.Record;
import com.tsl.prevention.modules.user.dao.RecordMapper;
import com.tsl.prevention.modules.user.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsl.prevention.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;

/**
 * <p>
 * 出行记录表 服务实现类
 * </p>
 *
 * @author Lionel
 * @since 2021-03-20
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private InfoMapper infoMapper;

    @Override
    public Object addByOpenId(@RequestParam("openId") String openId,@RequestParam("temp") String temp) {
        //查询用户
        Info info = infoMapper.selectOne(new QueryWrapper<Info>().eq("open_id", openId));
        if (Double.parseDouble(temp) > 37) {
            info.setState(2);

        } else {
            info.setState(1);
        }
        infoMapper.updateById(info);
        Record record = new Record();
        record.setRecordId(StringUtil.getGUID());
        record.setUserId(info.getId());
        record.setTemperature(temp);
        return recordMapper.insert(record);
    }

    @Override
    public IPage<HashMap<String, String>> getList(Page page,String openId) {
        return recordMapper.getList(page,openId);
    }
}
